package com.shoeHouse.order.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shoeHouse.order.model.OrderInfo;
import com.shoeHouse.order.service.OrderService;
import com.shoeHouse.user.model.UserInfo;
import com.shoeHouse.util.DataGrid;
import com.shoeHouse.util.FileUploadUtil;
import com.shoeHouse.util.IdWork;
import com.shoeHouse.util.PageHelper;
import com.shoeHouse.util.ResultInfo;



@Controller
@RequestMapping("/order")
public class OrderAction {
	@Autowired
	private OrderService orderService;
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {
  
    
		return "/pages/order/list.jsp";
	}

	@ResponseBody
	@RequestMapping("/view")
	public DataGrid view(HttpServletRequest request,HttpServletResponse response,Integer page,Integer rows,HttpSession session){
		
		//page表示当前页，rows表示一次查询的条数
		PageHelper pageHelper=new PageHelper();
		pageHelper.setCurrentPage(page);
		pageHelper.setPageSize(rows);
		
		//查询总数
		   DataGrid dataGrid=new DataGrid();
		   OrderInfo orderInfo=new OrderInfo();
		   UserInfo info=(UserInfo) session.getAttribute("userInfo");
		   orderInfo.setSeller(info.getId());
		List<OrderInfo> list=orderService.getAll(request,orderInfo,pageHelper);
		int count=orderService.count();
		dataGrid.setTotal(count);
		dataGrid.setRows(list);

		return dataGrid;
			
			
			
		}
	/**
	 * 确认订单
	 * @param map
	 * @return
	 */
	@ResponseBody
		@RequestMapping("/confirmRec")
	public ResultInfo confirmRec(String id){
		OrderInfo o=new OrderInfo();
		o.setId(id);
		o.setDeliverGoodsStatus("送货中");
		o.setPayStatus("待收货");
			orderService.update(o);
		ResultInfo resultInfo=new ResultInfo();
		return resultInfo;
	}
		
		
	
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		 
		return "/pages/user/save.jsp";
		
	}

	@ResponseBody
	@RequestMapping(value ="/addSave")
	public ResultInfo addSave(@Validated OrderInfo orderInfo,HttpServletRequest request,@RequestParam(value = "uploadFile", required = false) MultipartFile file){
		// UserInfo userInfo=new UserInfo();
		System.out.println(file != null);
		if(file != null){
			try {
				String icon_url = FileUploadUtil.saveFile(file, request,"photo");
				System.out.println(icon_url);
			
			} catch (Exception e) {
				
			}
		}
		
		
	       Date date=new Date();
	       orderInfo.setId(IdWork.nextId());
	
	     orderInfo.setGmtModified(date);
	
	     orderInfo.setStatus(Byte.parseByte("0"));
	     orderService.save(orderInfo);
	     ResultInfo result=new ResultInfo();
	     result.setSuccess(0);
		return result;
	}
	
	
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
//		map.put("saveUrl", "/manage/apartment/editSave");
	OrderInfo orderInfo=new OrderInfo();
	orderInfo.setId(id);
		OrderInfo orderInfos=orderService.getSingle(orderInfo);
		map.put("model",orderInfos);
	
		return "/pages/user/save.jsp";
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ResultInfo editSave(OrderInfo orderInfo,HttpServletRequest request) {
		orderInfo.setGmtModified(new Date());
		orderService.update(orderInfo);
		ResultInfo result=new ResultInfo();
		result.setSuccess(0);
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/delete")
	public String delete(String ids) {
		
		return null;
	}

	
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ResultInfo remove(String ids) {
		ResultInfo info=new ResultInfo();
       orderService.removeAll(ids);
		info.setMsg("正常");
		info.setSuccess(0);

		return info;
	}
	@ResponseBody
	@RequestMapping(value = "/updateStatus")
	public String updateStatus(String ids) {
		
		
		return null;
	}

}
